local Msg = require("core.Msg")
local YellowExcel = require("excel.Yellow")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local Buffer   = require("buffer.Buffer")
local ItemDefine = require("bag.ItemDefine")
local PfLogic = require("tencentApi.PfLogic")

--领取奖励
function getReward(human, type, index)
  if human.db.yellowVip ~= 1 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_CANNOT_GET_REWARD)
  end
  if type == 1 then
    if human.db.yellowNewReward == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if not BagLogic.checkCanAddItemList(human, YellowExcel.new[1].reward, #YellowExcel.new[1].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
    end
    human.db.yellowNewReward = ItemDefine.REWARD_ALREADY_GET
    for i=1, #YellowExcel.new[1].reward do
      BagLogic.addItem(human, YellowExcel.new[1].reward[i][1], 1, YellowExcel.new[1].reward[i][2], "yellow")
    end    
  elseif type == 2 then
    if human.db.yellowLvReward[index - 1] == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if human.db.lv < YellowExcel.lv[index].lv then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_HUMAN_LV_NOT_ENOUGH)
    end
    if not BagLogic.checkCanAddItemList(human, YellowExcel.lv[index].reward, #YellowExcel.lv[index].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
    end
    human.db.yellowLvReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
    for i=1, #YellowExcel.lv[index].reward do
      BagLogic.addItem(human, YellowExcel.lv[index].reward[i][1], 1, YellowExcel.lv[index].reward[i][2], "yellow")
    end    
  elseif type == 3 then
    if human.db.yellowDailyReward[index - 1] == ItemDefine.REWARD_ALREADY_GET then
      return
    end
    if index == 1 then
      if not BagLogic.checkCanAddItemList(human, YellowExcel.daily[human.db.yellowVipLv].reward, #YellowExcel.daily[human.db.yellowVipLv].reward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
      end
      human.db.yellowDailyReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
      for i=1, #YellowExcel.daily[human.db.yellowVipLv].reward do
        BagLogic.addItem(human, YellowExcel.daily[human.db.yellowVipLv].reward[i][1], 1, YellowExcel.daily[human.db.yellowVipLv].reward[i][2], "yellow")
      end
    elseif index == 2 then
      if human.db.yellowYearVip ~= 1 then
        return
      end
      if not BagLogic.checkCanAddItemList(human, YellowExcel.daily[1].yearReward, #YellowExcel.daily[1].yearReward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
      end
      human.db.yellowDailyReward[index - 1] = ItemDefine.REWARD_ALREADY_GET
      for i=1, #YellowExcel.daily[1].yearReward do
        BagLogic.addItem(human, YellowExcel.daily[1].yearReward[i][1], 1, YellowExcel.daily[1].yearReward[i][2], "yellow")
      end
    end    
  else
    return
  end
  sendRewardStatus(human, type)
end
--发送奖励列表
function sendRewardList(human, type)
  local sendMsg = Msg.gc.GC_YELLOW_REWARD_LIST
  local cache = {}
  sendMsg.type = type
  if type == 1 then
    sendMsg.rewardList[0] = #YellowExcel.new
    sendMsg.rewardList[1].condition = 0
    sendMsg.rewardList[1].reward[0] = #YellowExcel.new[1].reward
    for i=1, sendMsg.rewardList[1].reward[0] do
      Grid.create(cache, YellowExcel.new[1].reward[i][1], YellowExcel.new[1].reward[i][2])
      Grid.makeItem(cache, sendMsg.rewardList[1].reward[i], 0, human)
    end
    sendMsg.rewardList[1].attr[0] = 0
  elseif type == 2 then
    sendMsg.rewardList[0] = #YellowExcel.lv
    for i=1,sendMsg.rewardList[0] do
      sendMsg.rewardList[i].condition = YellowExcel.lv[i].lv
      sendMsg.rewardList[i].reward[0] = #YellowExcel.lv[i].reward
      for j=1, sendMsg.rewardList[i].reward[0] do
        Grid.create(cache, YellowExcel.lv[i].reward[j][1], YellowExcel.lv[i].reward[j][2])
        Grid.makeItem(cache, sendMsg.rewardList[i].reward[j], 0, human)
      end 
      sendMsg.rewardList[i].attr[0] = 0    
    end 
  elseif type == 3 then
    sendMsg.rewardList[0] = #YellowExcel.daily + 1
    for i=1,#YellowExcel.daily do
      sendMsg.rewardList[i].condition = 0
      sendMsg.rewardList[i].reward[0] = #YellowExcel.daily[i].reward
      for j=1, sendMsg.rewardList[i].reward[0] do
        Grid.create(cache, YellowExcel.daily[i].reward[j][1], YellowExcel.daily[i].reward[j][2])
        Grid.makeItem(cache, sendMsg.rewardList[i].reward[j], 0, human)
      end
      sendMsg.rewardList[i].attr[0] = 0    
    end
    sendMsg.rewardList[sendMsg.rewardList[0] ].reward[0] = #YellowExcel.daily[1].yearReward
    for i=1, #YellowExcel.daily[1].yearReward do
      Grid.create(cache, YellowExcel.daily[1].yearReward[i][1], YellowExcel.daily[1].yearReward[i][2])
      Grid.makeItem(cache, sendMsg.rewardList[sendMsg.rewardList[0] ].reward[i], 0, human)      
    end
    sendMsg.rewardList[sendMsg.rewardList[0] ].condition = 0
    sendMsg.rewardList[sendMsg.rewardList[0] ].attr[0] = 0 
  else
    sendMsg.rewardList[0] = #YellowExcel.other
    for i=1,sendMsg.rewardList[0] do
      sendMsg.rewardList[i].condition = 0
      sendMsg.rewardList[i].reward[0] = 0
      sendMsg.rewardList[i].attr[0] = #YellowExcel.other[i].attr
      for j=1, sendMsg.rewardList[i].attr[0] do
        sendMsg.rewardList[i].attr[j].key = YellowExcel.other[i].attr[j][1]
        sendMsg.rewardList[i].attr[j].value = YellowExcel.other[i].attr[j][2]
      end
    end 
  end
  Msg.send(sendMsg, human.fd)
  sendRewardStatus(human, type)
end
--发送状态
function sendRewardStatus(human, type)
  local sendMsg = Msg.gc.GC_YELLOW_REWARD_STATUS
  sendMsg.type = type
  if type == 1 then
    sendMsg.status[0] = #YellowExcel.new
    sendMsg.status[1] = human.db.yellowNewReward
  elseif type == 2 then
    sendMsg.status[0] = #YellowExcel.lv
    for i=1, sendMsg.status[0] do
      sendMsg.status[i] = human.db.yellowLvReward[i-1]
    end    
  elseif type == 3 then
    sendMsg.status[0] = 2
    for i=1, sendMsg.status[0] do
      sendMsg.status[i] = human.db.yellowDailyReward[i-1]
    end
  else
    return
  end
  Msg.send(sendMsg, human.fd)
end

function addBuffer(human)
  if human.db.yellowVip ~= 0 and PfLogic.isYellowShow(human) then
    for i=1, #YellowExcel.other[1].buf do
      Buffer.add(YellowExcel.other[1].buf[i], human, human)
    end
  end
end

function clearStatus(human)
  human.db.yellowDailyReward[0] = 0
  human.db.yellowDailyReward[1] = 0
  for i=1,3 do
    sendRewardStatus(human, i)
  end
end